var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    list: [],
    pageNo: 1,
    pageSize: 10,
    totalPage: 1,
    lat: "",
    lon: "",
    titleOne: "类型",
    titleTwo: "筛选",
    selectListOne: [],
    selectListTwo: ["最新", "最热", "最近"],
    showId: -1,
    businessType: '',//类型id
  },
  goodsList: function () {
    let that = this;
    wx.getLocation({
      success: function (res) {
        console.log("已获取到经纬度");
        that.setData({ lat: res.latitude })
        that.setData({ lon: res.longitude })
        that.list();
      },
      fail: function (res) {
        console.log('拒绝获取经纬度');
        that.list();
      }
    })
  },
  //特价购列表
  list: function () {
    var that = this;
    var params = new Object();
    params.pageNo = this.data.pageNo;
    params.pageSize = this.data.pageSize;
    //params.type = 1;//推广类型 1,特价购
    params.lat = this.data.lat;
    params.lon = this.data.lon;
    if (this.data.businessType.length != '') {
      params.businessType = this.data.businessType;
    }
    let titleTwo = this.data.titleTwo;//1,最新 2,最热 3,最近
    switch (titleTwo) {
      case '最新': params.orderType = 1; break;
      case '最热': params.orderType = 2; break;
      case '最近': params.orderType = 3; break;
    }
    let openId = wx.getStorageSync("openidKey");
    console.log("param:" + JSON.stringify(params));
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/goods/getGoodsList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        //console.log("特价购：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data.list;
          // that.setData({ list: rows })
          that.setData({ totalPage: res.data.totalPage })
          if (that.data.pageNo == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
          that.commission();
        }
      }
    })
  },
  commission: function () {
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    let rows = this.data.list;
    if (rows != null && rows.length > 0) {
      for (let i = 0; i < rows.length; i++) {
        let row = rows[i];
        wx.request({
          header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
          url: httpsUrl + "/goodsCommission/getCommission",
          dataType: "json",
          method: "POST",
          data: { "goodsNo": rows[i].code },
          success: function (res) {
            res = res.data;
            //console.log(i+"佣金：" + JSON.stringify(res));
            if (res.result == 0) {
              row.commissionStr = res.data;
              if (rows[i].promotion_describe == null) { row.promotion_describe = "" }
              rows[i] = row;
              that.setData({ list: rows })
            }
          }
        })
      }
    }
  },
  goodsInfo: function (e) {
    let id = e.currentTarget.id;
    wx.setStorageSync("goodsCode", id);
    wx.navigateTo({
      url: '../../shop/goodsInfo/goodsInfo',
    })
  },
  //商家类型
  types: function () {
    var that = this;
    var params = new Object();
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/business/getBusinessType",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("类型：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data;
          that.setData({ selectListOne: rows })
        }
      }
    })
  },
  //设置显示块id
  showSearch: function (e) {
    let id = e.currentTarget.id;
    if (id == this.data.showId) {
      this.setData({ showId: -1, })
    } else {
      this.setData({ showId: id })
    }
    console.log(this.data.showId)
  },
  //查询条件选择事件
  selectItem: function (e) {
    let text = e.currentTarget.dataset.text;
    let id = e.currentTarget.id;
    if (id == 1) { //text为index,-1为全部
      if (text == '-1') {
        this.setData({ titleOne: '全部', businessType: '' })
      } else {
        let index = Number(text);
        let rows = this.data.selectListOne;
        let name = rows[index].name;
        let typeId = rows[index].id;
        this.setData({ titleOne: name, businessType: typeId, })
      }
    } else if (id == 2) {
      if (text == '-1') {
        this.setData({ titleTwo: '全部' })
      } else {
        this.setData({ titleTwo: text })
      }
    }
    this.setData({ showId: -1 })
    this.list();
  },
  onLoad: function () {
    this.goodsList();
    this.types();
  },
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    pageNo = pageNo + 1;
    if (pageNo > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ pageNo: pageNo })
      this.list();
    }
  },
})